package com.imti.service.impl;


import com.imti.bean.DeskInfo;
import com.imti.bean.Doctor;
import com.imti.bean.Duty;
import com.imti.bean.pojo.Dutya;
import com.imti.mapper.DoctorDao;
import com.imti.mapper.DutyaDao;
import com.imti.service.DutyService;
import com.imti.util.PageBean;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
@Transactional
public class DutyServiceImpl implements DutyService {

    @Autowired
    private DutyaDao dutyaDao;

    @Autowired
    private DoctorDao doctorDao;


    @Override
    public PageBean pageDuty(Map<String, Object> map) {
        // 1、查询总条数
        Dutya dutya = (Dutya) map.get("dutya");
        PageBean pageBean = (PageBean) map.get("page");
        Integer pageLimt = (pageBean.getPage() == null) ? 5 : pageBean.getPage();
        Integer currNum = (pageBean.getCurrNo() == null) ? 1 : pageBean.getCurrNo();
        int totalCount = dutyaDao.dutyConut(dutya);
        // 2、查询列表
        PageBean page = new PageBean(pageLimt, currNum);
        map.put("page", page);
        List<Dutya> list = dutyaDao.selectAll(map);
        // 3、封装pagebean
        PageBean npage = new PageBean(pageLimt, totalCount, currNum, list, "/duty/page", dutyParams(dutya));
        return npage;
    }
    private String dutyParams(Dutya dutya) {
        StringBuilder sb = new StringBuilder();
        if (StringUtils.isNotBlank(dutya.getDutyDate())) {
            sb.append("&dutyDate=").append(dutya.getDutyDate());
        }
        if (StringUtils.isNotBlank(dutya.getDocName())) {
            sb.append("&docName=").append(dutya.getDocName());
        }
        return sb.toString();
    }


    /*添加医生值班信息*/
    @Override
    public int insert(Dutya dutya) {
        return dutyaDao.insertAll(dutya);
    }

    /*查询全部医生姓名和ID，doctorDao下的findAllDoctor();*/
    @Override
    public List<Doctor> fillAllDoctor() {
        return doctorDao.findAllDoctor();
    }

    /**
     * 查询所有科室，doctorDao下的findAllDesk()方法;
     */
    public List<DeskInfo> findAllDesk(){
        return doctorDao.findAllDesk();
    }
    /*根据值班id和医生id修改*/
    @Override
    public int updateDuty(Dutya dutya) {
        return dutyaDao.update(dutya);
    }


    /* 根据值班id查询值班信息*/
    @Override
    public List<Dutya> selectByDutyId(Dutya dutyId) {
        return dutyaDao.selectOne(dutyId);
    }

}
